package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class ProductContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProductContainer
{
    private ArrayList<Product> productContainer;
    private static ProductContainer instance;
    private Product pObject; 

    public ProductContainer()
    {
        productContainer = new ArrayList<Product>();
    }

    public static ProductContainer getInstance()
    {
        if(instance == null)
        {
            instance = new ProductContainer();
        }
        return instance;
    }

    public Product findProductByBarCode(int barcode)
    {
        pObject = null;
        int index = 0;
        boolean found = false;
        while(index < productContainer.size() && !found)
        { 
            if(productContainer.get(index).getBarCode() == barcode)
            {
                pObject = productContainer.get(index);
                found = true;
            }
            else
            {
                index++;
            }
        }//end while
        return pObject;
    }

    public ArrayList<Product> getProducts()
    {
        return productContainer;
    }
    
    public Product findProductByName(String name)
    {
        pObject = null;
        int index = 0;
        boolean found = false;
        while(index < productContainer.size() && !found)
        { 
            if(productContainer.get(index).getProductName().equals(name))
            {
                pObject = productContainer.get(index);
                found = true;
            }
            else
            {
                index++;
            }
        }//end while
        return pObject;
    }
    
    public boolean addProduct(Product p)
    {
        boolean added = false;
        if(findProductByBarCode(p.getBarCode()) == null)
        {
            productContainer.add(p);
            added = true;
        }
        return added;
    }

    public boolean deleteProductByBarCode(int barcode)
    {
        if(productContainer.contains(findProductByBarCode(barcode)))
        {
            productContainer.remove(findProductByBarCode(barcode));
            return true;
        }
        else
        {
            return false;
        }
    }
}
